var config;

fetch("./config.json")
.then(response => response.json())
.then(data => {
    config = data;
    get_history()
})
.catch(error => {
    console.error("Error:", error);
});

function get_history(){
    fetch("http://" + config.LLMHost.host + ":" + config.LLMHost.port + "/getHistory", {
        method: "GET",
    })
    .then((response) => {
        if (!response.ok) {
            throw new Error("Network response was not ok: " + response.statusText);
        }
        return response.json();
    })
    .then((data) => {
        console.log(data)
        let pdf_src = "./public/pdfjs/web/viewer.html?file=http://" + config.LLMHost.host + ":" + config.LLMHost.port + data.file_path;
        iframe.src = pdf_src
        for (let i = 0; i < data.history.length; i ++){
            let message = data.history[i]
            if (message.role === 'assistant'){
                console.log(message.content)
                displayAIMessage(create_p_BlockInfo(message.content))
            }else if (message.role === 'user') {
                displayMessage(message.content, 'user')
            }
        }
    })
    .catch((error) => {
        console.error("Error:", error)
        displayErrorMessage(error)
    })
}